using System.Linq;
using JetBrains.Annotations;
using Dyn.News.Services;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Environment;
using Orchard.Tasks;

namespace Dyn.News.Routing {
    [UsedImplicitly]
    public class CategoryPathConstraintUpdator : IOrchardShellEvents, IBackgroundTask {
        private readonly ICategoryPathConstraint _categoryPathConstraint;
        private readonly ICategoryervice _Categoryervice;

        public CategoryPathConstraintUpdator(ICategoryPathConstraint categoryPathConstraint, ICategoryervice Categoryervice) {
            _categoryPathConstraint = categoryPathConstraint;
            _Categoryervice = Categoryervice;
        }
        
        void IOrchardShellEvents.Activated() {
            Refresh();
        }

        void IOrchardShellEvents.Terminating() {
        }

        void IBackgroundTask.Sweep() {
            Refresh();
        }

        private void Refresh() {
            _categoryPathConstraint.SetPaths(_Categoryervice.Get().Select(b => b.As<IRoutableAspect>().Slug));
        }
    }
}